package com.sss.otep.orm.repository;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sss.otep.orm.model.Member;
import com.sss.otep.orm.model.RecordStatus;

@Repository
public class MemberRepository extends BaseRepository<Member> {
	@Override
	protected Class<Member> getPersistantClass() {
		return Member.class;
	}

	/**
	 * Find members by parameters
	 * 
	 * @param id
	 * @param idCard
	 * @param name
	 * @param surname
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Member> findByCriteria(Integer id, String idCard, String name,
			String surname) throws Exception {
		String stm = "select m from Member as m where m.recordInfo.recordStatus = :rec";

		Map<String, Object> param = new Hashtable<String, Object>();
		param.put("rec", RecordStatus.N);
		if (id != null) {
			stm = stm.concat(" and m.cpkId = :id");
			param.put("id", id);
		}
		if (!StringUtils.isBlank(idCard)) {
			stm = stm.concat(" and m.cpkMemIdcard = :idcard");
			param.put("idcard", idCard);
		}
		if (!StringUtils.isBlank(name)) {
			stm = stm.concat(" and m.cpkMemName = :name");
			param.put("name", name);
		}
		if (!StringUtils.isBlank(surname)) {
			stm = stm.concat(" and m.cpkMemSurname = :surname");
			param.put("surname", surname);
		}
		Query query = getCurrentSession().createQuery(stm);
		for (String key : param.keySet()) {

			query.setParameter(key, param.get(key));
		}

		return (List<Member>) query.list();
	}
}
